using System;
using System.Drawing;
using FireStarterModeller.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Relationships
{
    /// <summary>
    /// This is a class for Testing the DMRelationshipsControl.
    /// </summary>
    [TestFixture]
    public class TestDMRelationshipsControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        [Test]
        public void TestConstruction()
        {
            //---------------Set up test pack-------------------

            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            DMRelationshipsControl control = new DMRelationshipsControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof (DMRelationshipsControl), control);
            Assert.IsInstanceOf(typeof (IFormControl), control);
            Assert.AreEqual(3, control.Controls.Count);
            Assert.IsInstanceOf(typeof (IBusinessObjectControl), control.Controls[0]);
            Assert.IsInstanceOf(typeof (IBOColSelectorControl), GetParentChildSelectorControl(control));
            Assert.IsNotNull(control.RelationshipSelector);
            Assert.IsNotNull(control.RelationshipEditorControl);
        }

        private static IBOParentChildrenSelector GetParentChildSelectorControl(DMRelationshipsControl control)
        {
            return (IBOParentChildrenSelector) control.Controls[2];
        }

        [Test]
        public void TestSetBusinessObject_LoadsRelationshipInfos()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithRelationships();
            DMRelationshipsControl control = new DMRelationshipsControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            IBusinessObjectCollection selectorCollection = control.RelationshipSelector.BusinessObjectCollection;
            Assert.AreEqual(dmClass.OwnerRelationships, selectorCollection);
        }

        [Test]
        public void TestSetBusinessObject_ToNull_LoadsEmptyCollection()
        {
            //---------------Set up test pack-------------------
            DMRelationshipsControl control = new DMRelationshipsControl(new ControlFactoryWin());
            IBOParentChildrenSelector boSelector = GetParentChildSelectorControl(control);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            IBusinessObjectCollection gridCollection = boSelector.BusinessObjectCollection;
            Assert.IsNull(gridCollection);
        }

        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMRelationshipsControl control =
                new DMRelationshipsControl(new ControlFactoryWin());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetBusinessObject_EnablesControl()
        {
            //---------------Set up test pack-------------------
            DMClass info = TestUtilsShared.CreateSavedDMClassWithRelationships();
            DMRelationshipsControl control =
                new DMRelationshipsControl(new ControlFactoryWin());
            //--------------Assert Precondition ----------------
            Assert.IsFalse(control.Enabled);
            //---------------Execute Test ----------------------
            control.Class = info;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
        }

        [Test]
        public void TestSetBusinessObject_ToNull_DisablesControl()
        {
            //---------------Set up test pack-------------------
            DMClass info = TestUtilsShared.CreateSavedDMClassWithRelationships();
            DMRelationshipsControl control =
                new DMRelationshipsControl(new ControlFactoryWin());
            control.Class = info;
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetRelationshipInfo_SetsSelectedRelationshipInfo()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithRelationships();

            DMRelationshipsControl control = new DMRelationshipsControl(new ControlFactoryWin());
            IBOParentChildrenSelector boSelector = GetParentChildSelectorControl(control);
            control.Class = bo;
            DMRelationship relationship = bo.OwnerRelationships[1];

            //---------------Execute Test ----------------------
            boSelector.SelectedBusinessObject = relationship;

            //---------------Test Result -----------------------
            Assert.AreSame(relationship, boSelector.SelectedBusinessObject);
        }

        [Test]
        public void TestLinkBetweenRelationshipInfoAndRelatedPropertyInfoControls()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithRelationships();

            DMRelationshipsControl control = new DMRelationshipsControl(new ControlFactoryWin());
            control.Class = bo;
            DMRelationship dmRelationship = bo.OwnerRelationships[0];
            IBOParentChildrenSelector boSelector = GetParentChildSelectorControl(control);
            //---------------Execute Test ----------------------
            boSelector.SelectedBusinessObject = dmRelationship;
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, control.RelationshipEditorControl.BusinessObject);
        }

        [Test]
        [Ignore("This is for visual testing only")]
        public void Test_Visually()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = UITestPackBuilder.CreateDMSolution(0);
            DMClass parentClass = UITestPackBuilder.CreateDMClassWithObjectIdentity(solution, "Parent");
            DMClass childClass = UITestPackBuilder.CreateDMClassWithObjectIdentity(solution, "Child");
            DMRelationship relationship = UITestPackBuilder.CreateDMRelationshipAsOneToMany(parentClass, childClass);
            relationship.DeleteAction = DeleteParentAction.DoNothing;
            solution.Save();

            ControlFactoryWin controlFactory = new ControlFactoryWin();
            DMRelationshipsControl relationshipsControl = new DMRelationshipsControl(controlFactory);
            Action<DMClass> businessObjectSelected = dmClass => relationshipsControl.Class = dmClass;
            BusinessObjectCollection<DMClass> businessObjectCollection = solution.DefaultAssembly.Classes;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingFormWithSelector(controlFactory, relationshipsControl, 
                businessObjectCollection, businessObjectSelected, DockStyle.Fill);
            //---------------Test Result -----------------------
        }

/*        [Test]
        [Ignore("This is for visual testing only")]
        public void Test_Visually_WithDmClassSelector()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = UITestPackBuilder.CreateDMSolution(0);
            DMClass parentClass = UITestPackBuilder.CreateDMClassWithObjectIdentity(solution, "Parent");
            DMClass childClass = UITestPackBuilder.CreateDMClassWithObjectIdentity(solution, "Child");
            UITestPackBuilder.CreateDMRelationshipAsOneToMany(parentClass, childClass);
            solution.Save();

            ControlFactoryWin controlFactory = new ControlFactoryWin();
            DMRelationshipsControl relationshipsControl = new DMRelationshipsControl(controlFactory);
            
            IPanel panel = controlFactory.CreatePanel();
            DMClassSelectorControl selectorControl = new DMClassSelectorControl(controlFactory);


            panel.Size = new Size(relationshipsControl.Width + selectorControl.Width, relationshipsControl.Height);
            relationshipsControl.Dock = DockStyle.Fill;
            panel.Controls.Add(relationshipsControl);
            selectorControl.Dock = DockStyle.Left;
            panel.Controls.Add(selectorControl);
            selectorControl.Assembly = solution.DefaultAssembly;
            selectorControl.BusinessObjectSelected += (sender, e) =>
            {
                DMClass dmClass = e.BusinessObject as DMClass;
                relationshipsControl.Class = dmClass;
            };

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingForm(controlFactory, panel, DockStyle.Fill);
            //---------------Test Result -----------------------
        }*/

        
    }
}